package com.lyy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

import com.github.pagehelper.PageInfo;
import com.lyy.entity.Banner;

import com.lyy.Vo.BannerVo;
import com.lyy.Vo.BannerVoPage;
import com.lyy.entity.param.BannerInsertParam;
import com.lyy.entity.param.BannerUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.BannerService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/banner")
public class BannerController {

    public BannerService bannerService;


    @RequestMapping("/select-by-banner-id/{banner-id}")
    public Result selectByBannerId(@PathVariable("banner-id") Integer bannerId) {
        Banner banner = bannerService.selectByBannerId(bannerId);
        if (ObjectUtil.isEmpty(banner)) {
            return Result.failure();
        }
        BannerVo bannerVo = new BannerVo();
        BeanUtil.copyProperties(banner, bannerVo);
        return Result.success(bannerVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody BannerInsertParam bannerInsertParam) {
        Banner banner = BeanUtil.copyProperties(bannerInsertParam, Banner.class);
        int result = bannerService.insertByBannerId(banner);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody BannerUpdateParam bannerUpdateParam) {
        Banner banner = BeanUtil.copyProperties(bannerUpdateParam, Banner.class);
        int result = bannerService.updateByBannerId(banner);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/list")
    public Result list() {
        List<Banner> banners = bannerService.selectAllBanner();
        if (ObjectUtil.isEmpty(banners)) {
            return Result.failure();
        }
        List<BannerVo> bannerVos = BeanUtil.copyToList(banners, BannerVo.class);
        return Result.success(bannerVos);
    }

    @RequestMapping("/delete-by-banner-id/{banner-id}")
    public Result deleteByBannerId(@PathVariable("banner-id") Integer bannerId) {
        int result = bannerService.deleteByBannerId(bannerId);
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/delete-by-banner-ids")
    public Result deleteByBannerIds(@RequestParam("banner-ids") Integer[] bannerIds) {
        int result = bannerService.deleteByBannerIds(List.of(bannerIds));
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/page")
    public Result page(@RequestParam("page") Integer page,
                       @RequestParam("size") Integer size) {
        PageInfo<Banner> result = bannerService.selectBypage(page, size);
        if (ObjectUtil.isEmpty(result)) {
            return Result.failure();
        }
        BannerVoPage bannerVoPage=new BannerVoPage();
        bannerVoPage.setPage(result.getPageNum());
        bannerVoPage.setSize(result.getPageSize());
        bannerVoPage.setTotal(result.getTotal());
        bannerVoPage.setPages(result.getPages());
        bannerVoPage.setPageNums(result.getNavigatepageNums());
        bannerVoPage.setBannerVos(BeanUtil.copyToList(result.getList(),BannerVo.class));
        return Result.success(bannerVoPage);
    }
    @RequestMapping("/upload-banner")
    public Result uploadBanner(@RequestParam("banner-file") MultipartFile bannerFile,
                               @RequestParam("banner-id") Integer bannerId) {
        int result = bannerService.uploadBanner(bannerFile, bannerId);
        return result > 0 ? Result.success() : Result.failure();
    }

}
